package per.wsj.stickyheader

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_list.*
import per.wsj.stickyheader.decoration.DecorationAdapter
import per.wsj.stickyheader.decoration.PowerGroupListener
import per.wsj.stickyheader.decoration.SectionDecoration

class DecorationActivity : AppCompatActivity() {

    val mData = ArrayList<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list)

        for (i in 0..50) {
            mData.add("item:$i")
        }
        rv.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        rv.adapter = DecorationAdapter(mData)
        initDecoration()
    }

    private fun initDecoration() {
        val decoration = SectionDecoration(object : PowerGroupListener {
            override fun getGroupName(position: Int): String? {
                //获取组名，用于判断是否是同一组
                return if (position < mData.size) {
                    getSectionType(position)
                } else null
            }

            override fun getGroupView(position: Int): View? {
                //获取自定定义的组View
                return if (position < 50) {
                    val view: View =
                        layoutInflater.inflate(R.layout.recycler_home_title_star, null, false)
                    (view.findViewById(R.id.tvHeaderTitle) as TextView).text =
                        getSectionType(position)
                    view
                } else {
                    null
                }
            }
        })
        rv.addItemDecoration(decoration)
    }

    fun getSectionType(position: Int): String {
        return if (position < 20) {
            "已配对"
        } else {
            "新设备"
        }
    }
}